//初始化
window.onload = function (){
    changeStatus(window.location.href)
}

//选择条件切换
$(".project-search-map li").click(function (){
    $(this).addClass('on').siblings().removeClass("on");
    window.location.href = getSelect();
});

function getSelect(){
    let url = window.location.href;
    let arr = [];
    let select = [];

    //每次刷新条件后需清除URL参数
    arr = url.split("?");
    url = arr[0];
    //获取选中的参数
    $(".project-search-map ul").each(function (){
        let p = $(this);
        let _where = [],name,attr;

        _where['name'] = $(this).attr("class");
        p.find("li").each(function (){
            if ($(this).hasClass("on")){
                _where['attr'] = $(this).attr("data-id");
            }
        });
        select.push(_where)
    })
    //重新赋值url
    if (select.length > 1){
        let url1 = '';
        for (let i = 0; i < select.length;i++){
            url1 += select[i]['name'] + "=" + select[i]['attr'] + "&";
        }
        url = url+"?"+url1;

        return url.substr(0,url.length - 1);
    }
}

//页面div默认选中已选择的条件
function changeStatus(url){
    let params = url.split("?")[1];

    if (params) {
        _arr = params.split("&");
    }
    else {
        return false;
    }

    for (let i = 0; i<_arr.length;i++){
        let name,attr;
        let _name = $(".project-search-map >ul").eq(i).attr("class");
        let div_attr = $(".project-search-map > ul").eq(i).find("li");
        let _attr;

        name = _arr[i].split("=")[0];
        attr = _arr[i].split("=")[1];
        div_attr.each(function (){
            let obj = $(this)
            _attr = obj.attr("data-id");
            if (name == _name &&attr == _attr){
                obj.addClass("on").siblings().removeClass("on");
            }
        });
    }
}

